package com.estronger.shopmanage.view;

/**
 * Created by Administrator on 2017-03-29.
 */

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.ui.measure.MeasureActivity;
import com.estronger.shopmanage.utils.ScreenUtils;
import com.soonfor.evaluate.tools.CommonUtils;

public class SelectPopupWindow extends PopupWindow {
    private View conentView;
    LinearLayout llfApproveDate;
    LinearLayout llfAll;
    Context mContext;
    MeasureActivity mActivity;
    TextView tvall, txtdate;
    static Window win;
    RelativeLayout mSearchtoolbar;

    @SuppressWarnings("deprecation")
    public SelectPopupWindow(final Activity context, String selectText, Window window, boolean isNeedShow, RelativeLayout searchtoolbar) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContext = context;
        mActivity = (MeasureActivity) context;
        win = window;
        mSearchtoolbar = searchtoolbar;
        conentView = inflater.inflate(R.layout.select_yishen_popupwindow, null);
        llfAll = (LinearLayout) conentView.findViewById(R.id.popll0);
        llfApproveDate = (LinearLayout) conentView.findViewById(R.id.popll1);
        tvall = (TextView) conentView.findViewById(R.id.tvall);
        txtdate = (TextView) conentView.findViewById(R.id.txtdate);
        tvall.setText("全部");
        txtdate.setText("日期");
        llfAll.setBackgroundColor(Color.rgb(255, 255, 255));
        llfApproveDate.setBackgroundColor(Color.rgb(255, 255, 255));
        if (selectText.equals("日期")) {
            txtdate.setTextColor(Color.rgb(211, 35, 35));
        } else if (selectText.equals("全部")) {
            tvall.setTextColor(Color.rgb(211, 35, 35));
        }
//        int h = context.getWindowManager().getDefaultDisplay().getHeight();
//        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ScreenUtils.dip2px(mContext, 86));
        //this.setWidth(w/4);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setOnDismissListener(new poponDismissListener());
        this.setBackgroundDrawable(dw);
        this.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果

//        if (isNeedShow) {
//            backgroundAlpha(0.5f);
//        } else {
//            backgroundAlpha(1f);
//        }

        //mActivity.showHighLight(conentView);
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, 0, 0);
        } else {
            this.dismiss();
        }
    }

    /**
     * 添加弹出的popWin关闭的事件，主要是为了将背景透明度改回来
     */
    class poponDismissListener implements PopupWindow.OnDismissListener {

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public static void backgroundAlpha(float bgAlpha) {
//        WindowManager.LayoutParams lp = win.getAttributes();
//        lp.alpha = bgAlpha; //0.0-1.0
//        win.setAttributes(lp);
    }

    public void loadDefult(final TextView tv, final EditText search_box, final RelativeLayout dateSelect, final int type) {
        switch (type) {
            case 0:
   
                break;
            case 1:
                tv.setText("日期");
                search_box.setEnabled(false);
                search_box.setHint("");
                dateSelect.setVisibility(View.VISIBLE);
                search_box.setText("");
                break;
        }
    }

    public String showTextView(final TextView tv, final EditText search_box, final RelativeLayout dateSelect) {

        llfAll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dismiss();
                backgroundAlpha(1f);
                tv.setText("全部");
                search_box.setEnabled(true);
                dateSelect.setVisibility(View.GONE);
                search_box.setText("");
            }
        });

        llfApproveDate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dismiss();
                backgroundAlpha(1f);
                tv.setText("日期");
                search_box.setEnabled(false);
                search_box.setHint("");
                dateSelect.setVisibility(View.VISIBLE);
                search_box.setText("");
            }
        });

        return tv.getText().toString();
    }

}
